<?php

class Sistema_ModulosController extends App_Controller_Action
{
   
    private $_table     = 'MODULOS';
    private $_pk        = 'MOD_ID';
    private $_order_col = 'MOD_NOM';
    private $_order     = 'ASC';
    private $_sta_col   = 'MOD_ATIVO';
    private $obj        = '';
    private $form       = '';
    private $_file_col  = 'MOD_FILE'; 
    public  $dir = 'public/upload/modulos/';
    public  $db = '';
    
    public function init()
    {
        $this->obj = new Sistema_Model_DbTable_Modulos();
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Modulos();
        $this->db = Zend_Db_Table_Abstract::getDefaultAdapter();
    }

    public function indexAction() {
        
        Zend_Loader::loadClass('tools','library');
        
        $search = $this->_getParam('inp-search', '');
        $this->view->search = $search;
        $where = 'WHERE md.MOD_ID > 0 and md.MEN_ID = men.MEN_ID  ';
        if(strlen($search) > 1){
            $where .= ' and md.MOD_NOME like "%'.$search.'%"';
        }
        
        $dscfiltro = $this->_getParam('dscfiltro', '');
        if($dscfiltro > '') $where .= ' and (md.MOD_NOME like "%'. $dscfiltro.'%")';
        $this->view->dscfiltro = $dscfiltro;
        

        //$result = $obj->fetchAll($find);
        //$result = $this->obj->fetchAll($this->obj->select()->where($where)->order(array('MEN_ID ASC','MOD_ORDMEN ASC')));
        $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' md, MENU men  '.$where.' ORDER BY men.MEN_ORD ASC, md.MOD_ORDMEN ASC';
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result,20);
         
    }
    
    
    public function editAction() {
        $form = new Sistema_Form_Modulos();
        $form->submit->setLabel('Gravar');
        
        Zend_Loader::loadClass('imagem','library');
        
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if ($form->isValid($formData)) {
                    $id = (int) $form->getValue('MOD_ID');

                    $returnImg = imagem::upload($this->_file_col,$this->dir,$id);
                    $returnImg2 = imagem::upload($this->_file_col.'2',$this->dir,$id.'_');

                    $data = array(
                        'MOD_NOME'      => $form->getValue('MOD_NOME'),
                        'MOD_ARQUIVO'   => $form->getValue('MOD_ARQUIVO'),	
                        'MEN_ID'        => $form->getValue('MEN_ID'),	
                        'MOD_ICONE'     => $form->getValue('MOD_ICONE'),	
                        'MOD_ATIVO'     => $form->getValue('MOD_ATIVO'),
                        'MOD_HOME'      => $form->getValue('MOD_HOME'),
                        'MOD_MENU'      => $form->getValue('MOD_MENU'),
                        'MOD_ORDMEN'    => $form->getValue('MOD_ORDMEN'),
                        'MOD_ORDMENINI' => $form->getValue('MOD_ORDMENINI'),
                    );
                    $obj = new Sistema_Model_DbTable_Modulos();

                    if ($id == 0){
                        $id = $obj->insert($data);
                        $id =  $obj->getAdapter()->lastInsertId();                    
                        imagem::rename($id,$this->dir,$returnImg['filenew']);
                        imagem::rename($id.'_',$this->dir,$returnImg2['filenew']);

                        $permisso = new Sistema_Model_DbTable_PermissaoHasModulo();
                        $data_permissao = array(
                            'PER_ID'  => 1,
                            'MOD_ID'  => $id,
                            'MOD_ADD' => 1,
                            'MOD_EDT' => 1,
                            'MOD_DEL' => 1,
                        );
                        $permisso->insert($data_permissao);
                    }
                    else
                        $obj->update($data, 'MOD_ID = ' . $id);

                    //width: 378px;
                    //height: 250px;

                    if(strlen($_FILES[$this->_file_col]['tmp_name']) > 1){
                        $img = glob($this->dir.$id.'.*');
                        foreach($img as $filetemp) $filename = $filetemp;
                        imagem::reizeFull($filename,$filename, '378', '235');
                    }

                    if(strlen($_FILES[$this->_file_col.'2']['tmp_name']) > 1){
                        $img = glob($this->dir.$id.'_.*');
                        foreach($img as $filetemp) $filename = $filetemp;
                        imagem::reizeFull($filename,$filename, '378', '235');
                    }

                    $this->_helper->redirector('index');
                } else {
                    $form->populate($formData);
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                // echo $ex->getMessage();
                //$this->form->populate($formData);
                $this->view->data = $formData;
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $obj = new Sistema_Model_DbTable_Modulos();
                $form->populate($obj->fetchRow('MOD_ID = ' . $id)->toArray());
            }
        }
    }

    public function deleteAction() {
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            
            $permisso = new Sistema_Model_DbTable_PermissaoHasModulo();
            $permisso->delete('MOD_ID = '.(int)$id);
            
            $this->obj->delete('MOD_ID = '.(int)$id);
            
            $this->_helper->redirector('index');
        } else {
            $id = $this->_getParam('id', 0);
            $obj = new Sistema_Model_DbTable_Modulos();
            $form->populate($obj->fetchRow('MOD_ID = ' . $id)->toArray());
        }
    }

   



}

